package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.AppItem;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.utils.Config;
import artid.com.arttogo.utils.NetUtils;

public class FindPasswordActivity extends BaseActivity implements
		OnClickListener {
	private TextView mResetPass;
	private Intent mIntent;
	private EditText phone, code;
	private TextView setCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_password);
		initView();
	}

	private CountDownTimer timer = new CountDownTimer(60000, 1000) {

		@Override
		public void onTick(long millisUntilFinished) {
			setCode.setText((millisUntilFinished / 1000) + "秒后可重发");
			setCode.setBackgroundResource(R.drawable.btn_hui);
			setCode.setClickable(false);
		}

		@Override
		public void onFinish() {
			setCode.setEnabled(true);
			setCode.setBackgroundResource(R.drawable.btn_orange);
			setCode.setClickable(true);
			setCode.setText("获取验证码");
		}
	};

	private void initView() {
		mContext = this;
		mBack = (ImageView) findViewById(R.id.back_btn);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		setCode = (TextView) findViewById(R.id.tv_set_code);
		phone = (EditText) findViewById(R.id.et_phone);
		code = (EditText) findViewById(R.id.et_code);
		mBack.setOnClickListener(this);
		mTittle.setText("找回密码");
		mResetPass = (TextView) findViewById(R.id.reset_pass);
		mResetPass.setOnClickListener(this);
		setCode.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.reset_pass:
			verCode();
			break;
		case R.id.back_btn:
			finish();
		case R.id.tv_set_code:
			if (!NetUtils.isMobileNO(phone.getText().toString())) {
				Toast.makeText(mContext, "手机号格式不正确，请重新输入~", 2).show();
				return;
			}
			VolleyBasic.getInstance(FindPasswordActivity.this).post(Cache.sendresetcode,
					VolleyMap.getInstance().sendresetcode(phone.getText().toString()), AppItem.class, new VolleyCallback() {
						
						@Override
						public void onSuccess(VolleyData vd) {
							// TODO Auto-generated method stub
							Config.showTip(mContext, "下发验证码成功！");
							timer.start();
						}
						
						@Override
						public void onError(VolleyError volleyError) {
							// TODO Auto-generated method stub
							
						}
					});
		default:
			break;
		}
	}

	/**
	 * 
	 */
	private void verCode() {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(phone.getText().toString())) {
			Toast.makeText(mContext, "手机号不能为空~", 2).show();
			return;
		} else if (!NetUtils.isMobileNO(phone.getText().toString())) {
			Toast.makeText(mContext, "手机号格式不正确，请重新输入~", 2).show();
			return;
		} else if (TextUtils.isEmpty(code.getText().toString())) {
			Toast.makeText(mContext, "验证码不能为空~", 2).show();
			return;
		}
		VolleyBasic.getInstance(FindPasswordActivity.this).post(Cache.code, 
				VolleyMap.getInstance().code(phone.getText().toString(), code.getText().toString()), AppItem.class, new VolleyCallback() {
					
					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						mIntent = new Intent(mContext, ResetPasswordActivity.class);
						mIntent.putExtra("phone", phone.getText().toString());
						mIntent.putExtra("code", code.getText().toString());
						startActivity(mIntent);
						finish();
					}
					
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						
					}
				});
	}

}
